//
//  BSSettingViewController.m
//  BaiSi
//
//  Created by Sycamore on 16/4/27.
//  Copyright © 2016年 Sycamore. All rights reserved.
//

#import "BSSettingViewController.h"
#import "BSFileSize.h"

@interface BSSettingViewController ()

@end


@implementation BSSettingViewController


static NSString * const ID = @"cell";


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    //获取缓存文件的大小
    
    //首先获取cache路径
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    
    NSInteger totleSize = [BSFileSize getFileSizeWithPath:cachePath];
    
    NSString *string = @"清楚缓存";
    
    if (totleSize >= 1000 * 1000) {
        string = [NSString stringWithFormat:@"%@(%.1fMB)",string,totleSize / 1000.0 / 1000.0];
    }
    else if (totleSize >= 1000){
        string = [NSString stringWithFormat:@"%@(%.1fKB)",string,totleSize / 1000.0];
    }else{
        string = [NSString stringWithFormat:@"%@(%.1ldB)",string,totleSize];
    }
    cell.textLabel.text = string;
        return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //删除所有的文件
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];
    
    [BSFileSize removeFilesAtPath:cachePath];

    //刷新数据
    [self.tableView reloadData];
}


@end
